package com.example.demo.mq.service;

import org.apache.activemq.artemis.jms.client.ActiveMQMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

//@Component
public class MailMessageListener {
    final Logger logger = LoggerFactory.getLogger(getClass());

    //@JmsListener(destination = "jinxin_queue_01", concurrency = "3-5")
    public void onMailMessageReceived(Message message) throws Exception {
        try {
            if(message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                logger.info("received message: " + textMessage.getText());
            } else if(message instanceof ActiveMQMessage) {
                ActiveMQMessage textMsg = (ActiveMQMessage) message;
                logger.info("received message: " + textMsg.getCoreMessage().getBodyBuffer().readString());
            }

        } catch (Exception e) {
            logger.error("Failed to get message from MQ, reason: {}", e.getMessage());
        }

    }

   // @JmsListener(destination = "jinxin_address_01", concurrency = "1")
    public void onMailMessageReceived2(Message message) throws Exception {
        try {
            if(message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                logger.info("received message: " + textMessage.getText());
            } else if(message instanceof ActiveMQMessage) {
                ActiveMQMessage textMsg = (ActiveMQMessage) message;
                logger.info("received message: " + textMsg.getCoreMessage().getBodyBuffer().readString());
            }

        } catch (Exception e) {
            logger.error("Failed to get message from MQ, reason: {}", e.getMessage());
        }

    }
}